package com.gitee.secretopen.sso.service.impl;

import cn.chenc.framework.cache.util.RedisUtils;
import cn.chenc.framework.core.model.request.RequestHolder;
import cn.chenc.framework.core.model.request.RequestUtil;
import cn.hutool.json.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.gitee.secretopen.sso.common.util.Query;
import com.gitee.secretopen.sso.component.TokenService;
import com.gitee.secretopen.sso.controller.form.PageInfo;
import com.gitee.secretopen.sso.db.pojo.Site;
import com.gitee.secretopen.sso.db.pojo.User;
import com.gitee.secretopen.sso.service.SiteService;
import org.apache.commons.lang3.ObjectUtils;
import org.aspectj.lang.JoinPoint;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;
import java.util.List;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gitee.secretopen.sso.db.dao.LogMapper;
import com.gitee.secretopen.sso.db.pojo.Log;
import com.gitee.secretopen.sso.service.LogService;
/**
 * @Author Xjt
 * @Date 2021/2/26 18:53
 * @Version 1.0
 */
@Service
public class LogServiceImpl extends ServiceImpl<LogMapper, Log> implements LogService{

    @Autowired
    private TokenService tokenService;
    @Autowired
    private SiteService siteService;

    @Override
    public PageInfo<Log> page(Log params) {
        IPage<Log> page = this.page(
                new Query<Log>().getPage(params),
                new QueryWrapper<Log>()
                        .between(!ObjectUtils.isEmpty(params.getStartDate()),
                                "create_time",params.getStartDate(),params.getEndDate())
                        .eq("is_delete",0)
        );
        return PageInfo.getInstance(page);
    }

    @Override
    public List<Log> downloadLogs() {
        List<Log> logList = this.list();
        return logList;
    }

    @Async
    @Override
    public void asyncSaveSystemLog(String bussinessName) {
        String ua = RequestUtil.getUa();
        Log log = new Log();
        HttpServletRequest request=RequestHolder.getRequest();
        User user=tokenService.getAdminLoginUser(request);
        log.setIp(RequestUtil.getIp());
        if (user != null) {
            log.setInfo(String.format("用户: [%s] | 操作: %s", user.getName(), bussinessName));
        } else {
            log.setInfo(String.format("访客: [%s] | 操作: %s", log.getIp(), bussinessName));
        }
        log.setCreateTime(LocalDateTime.now());
        this.save(log);

    }

    @Async
    @Override
    public void asyncSaveSsoLogin(JoinPoint joinPoint){
        String ua = RequestUtil.getUa();
        Log log = new Log();
        Object[] args = joinPoint.getArgs();
        Object arg = args[0];
        JSONObject user = new JSONObject(arg);
        log.setIp(RequestUtil.getIp());
        String siteId=user.getStr("siteId");
        Site site=siteService.getOne(new QueryWrapper<Site>().eq("site_id",siteId));
        if (user != null) {
            log.setInfo(String.format("用户: [%s] | 登录了: %s", user.getStr("name"), site.getSiteName()));
        } else {
            log.setInfo(String.format("访客: [%s] | 登录了: %s", log.getIp(), site.getSiteName()));
        }
        log.setCreateTime(LocalDateTime.now());
        this.save(log);
    }


}
